package com.ysx.behaviour.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysx.behaviour.dto.LikesBehaviourDto;
import com.ysx.behaviour.dto.UpdateArticleMess;
import com.ysx.behaviour.mapper.ApLikesBehaviorMapper;
import com.ysx.behaviour.pojo.ApBehaviorEntry;
import com.ysx.behaviour.pojo.ApLikesBehavior;
import com.ysx.behaviour.service.ApBehaviorEntryService;
import com.ysx.behaviour.service.ApLikesBehaviorService;
import com.ysx.common.constants.BusinessConstants;
import com.ysx.common.constants.SystemConstants;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.exception.ToutiaoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author ljh
 * @since 2021-10-07
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void like(LikesBehaviourDto likesBehaviourDto) throws Exception {

        //1.检查 参数值
        if(likesBehaviourDto == null
                || likesBehaviourDto.getArticleId() == null
                || likesBehaviourDto.getType() > 2
                || likesBehaviourDto.getType() < 0
                || likesBehaviourDto.getOperation() < 0
                || likesBehaviourDto.getOperation() > 1){
            throw new ToutiaoException("错误的参数");
        }

        //2.先获取当前用户的ID 如果是0 标识匿名用户 如果不是0 就是真实的用户
        String userInfo = RequestHeaderInfoUtils.getHeaderUserId();
        ApBehaviorEntry entry = null;
        if(userInfo.equals("0")){
            //匿名用户
            entry = apBehaviorEntryService.findByUserIdOrEquipmentId(likesBehaviourDto.getEquipmentId(), SystemConstants.TYPE_E);
        }else{
            //真实的用户
            entry = apBehaviorEntryService.findByUserIdOrEquipmentId(Integer.valueOf(userInfo), SystemConstants.TYPE_USER);
        }
        if(entry==null){
            throw new ToutiaoException("实体对象不存在");
        }

        //2.添加数据到表中(添加 也有可能是更新)

        if(likesBehaviourDto.getOperation()==1) {


            //toto 发送消息
            UpdateArticleMess mess = new UpdateArticleMess();
            mess.setArticleId(likesBehaviourDto.getArticleId());//文章ID
            mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);//点赞

            //参数1  指定topic 参数2  指定key 参数3 指定发送的内容 就是value
            kafkaTemplate.send(BusinessConstants.MqConstants.HOT_ARTICLE_SCORE_TOPIC,
                    UUID.randomUUID().toString(),
                    JSON.toJSONString(mess));

            //查询是否存在点赞记录 如果有 则不用点赞
            //select * from xxx where entry_id=? and article_id=?
            QueryWrapper<ApLikesBehavior> queryWrapper =new QueryWrapper<ApLikesBehavior>();
            queryWrapper.eq("entry_id",entry.getId());
            queryWrapper.eq("article_id",likesBehaviourDto.getArticleId());
            ApLikesBehavior apLikesBehavior = this.getOne(queryWrapper);
            if (apLikesBehavior == null) {
                ApLikesBehavior entity = new ApLikesBehavior();
                entity.setOperation(likesBehaviourDto.getOperation());
                entity.setArticleId(likesBehaviourDto.getArticleId());
                entity.setEntryId(entry.getId());//根据用户ID 或者设备ID 从 实体表中获取实体对象 再获取到主键 设置到这里
                entity.setCreatedTime(LocalDateTime.now());
                entity.setType(0);
                //调用本类的通用service方法
                this.save(entity);
            } else {

                //如果不为空 那么就是有可能是取消点赞
                apLikesBehavior.setOperation(1);
                //更新
                this.updateById(apLikesBehavior);
            }
        }else{
            //取消点赞

            //更新  update xxx set operation=0 where entry_id=? and article_id=? and operation=1
            QueryWrapper<ApLikesBehavior> queryWrapper =new QueryWrapper<ApLikesBehavior>();
            queryWrapper.eq("entry_id",entry.getId());
            queryWrapper.eq("article_id",likesBehaviourDto.getArticleId());
            queryWrapper.eq("operation",1);
            ApLikesBehavior apLikesBehavior = this.getOne(queryWrapper);
            if(apLikesBehavior!=null){
                apLikesBehavior.setOperation(0);
                this.updateById(apLikesBehavior);
            }

        }
    }
}
